@extends('layouts.application')

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">广告管理</strong> /
                    <small>Ad List</small>
                </div>
            </div>

            <hr>
            @include('layouts.shared._flash')

            <div class="am-g">

                <form action="" method="get">
                    <div class="am-u-sm-12 am-u-md-6">
                        <div class="am-btn-toolbar">
                            <div class="am-btn-group am-btn-group-xs">
                                <a href="{{route('ad.advert.create')}}" class="am-btn am-btn-default"><span
                                            class="am-icon-plus"></span> 新增
                                </a>
                                <button type="button" class="delete_all am-btn am-btn-default"><span
                                            class="am-icon-trash-o"></span> 删除
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-form-group">
                            <select data-am-selected="{btnSize: 'sm',maxHeight: 260, searchBox: 1}">
                                <option value="option1">所有类别</option>
                                <option value="option2">IT业界</option>
                                <option value="option3">数码产品</option>
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" name="keyword" value="{{Request::input('keyword')}}"
                                   class="am-form-field">
                            <span class="am-input-group-btn">
                                <button class="am-btn am-btn-default" type="submit">搜索</button>
                                <a href="{{route('ad.advert.index')}}" class="am-btn am-btn-default">重置</a>
                            </span>
                        </div>
                    </div>
                </form>

            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>ID</th>
                                <th>缩略图</th>
                                <th>标题</th>
                                <th>所属分类</th>
                                <th>URL地址</th>
                                <th>描述</th>
                                <th>是否显示</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($adverts as $advert)
                                <tr data-id="{{$advert->id}}">
                                    <td>
                                        <input type="checkbox" name="check_id[]" value="{{$advert->id}}"
                                               class="checked_id"/>
                                    </td>
                                    <td>{{$advert->id}}</td>
                                    <td>
                                        <img src="{{$advert->thumb}}" alt="" style="width: 50px;height: 50px;">
                                    </td>
                                    <td>{{$advert->title}}</td>
                                    <td>{{$advert->category['name']}}</td>
                                    <td>
                                        <a href="{{$advert->url}}">{{$advert->url}}</a>
                                    </td>
                                    <td class="am-hide-sm-only">{{str_limit($advert->description,15,'...')}}</td>
                                    <td>
                                        {!! is_something($advert,'is_show') !!}
                                    </td>
                                    <td class="am-hide-sm-only">
                                        <input type="hidden" name="id[]" value="{{$advert->id}}">
                                        <input type="text" class="sort_order" name="sort_order[]"
                                               value="{{$advert->sort_order}}" style="width: 80px;">
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('ad.advert.edit',$advert->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <button class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$adverts->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{$adverts->appends(Request::all())->links()}}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //单个删除
            $('.delete').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);
                $.ajax({
                    type: 'DELETE',
                    url: '/admin/ad/advert/' + id,
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                });
                return false;
            })

            //上面选中，下面也全选
            $('.check_all').click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            })
            //多选删除
            $('.delete_all').click(function () {
                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('请你至少选择一条');
                    return false;
                }
                $.ajax({
                    type: 'DELETE',
                    url: '{{route('ad.advert.delete_all')}}',
                    data: $('.checked_id:checked').serialize(),
                    success: function () {
//                        $('.checked_id:checked').each(function () {
//                            $(this).parents('tr').fadeOut(400);
//                        })
                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                })
                return false;
            })

            //排序
            $('.sort_order').change(function () {
                var id = $(this).parents('tr').data('id');
                var sort_order = $(this).val();

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('ad.advert.sort_order')}}',
                    data: {id: id, sort_order: sort_order},
                    success: function () {
                        window.location.reload();
                    }
                })
            })

            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: '{{route('ad.advert.change_attr')}}',
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                })
            })

        })
    </script>
@endsection